
#include		"OptionMenu.hh"

OptionMenu::OptionMenu(sf::RenderWindow &app, IGUI::e_context &f, sf::Event& e, Gomoku::Data &i)
  : info(i), focus(f), action(), App(app), Event(e), Input(this->App.GetInput()), wait(1), idFont(0)
{
  if (!this->coche.LoadFromFile("sprites/checkcase.png"))
    throw ImgException("LoadFromFile(sprites/checkcase.png) in OptionMenu.cpp");
  if (!this->bgm.LoadFromFile("sprites/bgmainmenu.jpg"))
    throw ImgException("LoadFromFile(sprites/bgmainmenu.jpg) in OptionMenu.cpp");
  if (!this->lb.LoadFromFile("sprites/arrow_left_button.png"))
    throw ImgException("LoadFromFile(sprites/arrow_left_button.png) in OptionMenu.cpp");
  if (!this->rb.LoadFromFile("sprites/arrow_right_button.png"))
    throw ImgException("LoadFromFile(sprites/arrow_right_button.png) in OptionMenu.cpp");
  this->action[sf::Event::KeyPressed] = &OptionMenu::gere_key;
  this->action[sf::Event::MouseButtonPressed] = &OptionMenu::mouse;
  this->action[sf::Event::MouseButtonReleased] = &OptionMenu::mouser;
  this->action[sf::Event::Resized] = &OptionMenu::resized;
  this->items.push_back(new GObject(this->coche, 2));
  this->items.push_back(new GObject(this->coche, 2));
  this->items.push_back(new GObject(this->bgm));
  this->hbutton.push_back(new GObject(this->lb, 3));
  this->hbutton.push_back(new GObject(this->rb, 3));
  this->addPolice("sprites/epitech.ttf", "Epitech", 32);
  this->addPolice("sprites/Karate.ttf", "Karate", 40);
  this->addPolice("sprites/COMICATE.TTF", "Comicate", 33);
  this->addPolice("sprites/Little Snorlax.ttf", "Little Snorlax", 40);
  this->addPolice("sprites/Christmas Snow.ttf", "Christmas Snow", 40);
  this->addPolice("sprites/AlphaWood.ttf", "AlphaWood", 40);
  this->addPolice("sprites/HARRYP__.TTF", "HARRY POTTER", 60);
  this->addPolice("sprites/Death Branch.ttf", "Death Branch", 40);
  this->hbutton[1]->setId(1);
  this->items[0]->setFocus(IGUI::OPTIONMENU);
  this->items[1]->setFocus(IGUI::OPTIONMENU);
  this->items[0]->setPos(this->App.GetWidth() / 2 + 170, 200);
  this->items[1]->setPos(this->App.GetWidth() / 2 + 170, 300);
  this->items[2]->resize(this->App.GetWidth(), this->App.GetHeight());
  this->items[2]->setPos(-1, -1);
  this->hbutton[0]->setPos(this->App.GetWidth() / 2 - 260, 420);
  this->hbutton[1]->setPos(this->App.GetWidth() / 2 + 220, 420);
  this->hbutton[0]->resize(50, 50);
  this->hbutton[1]->resize(50, 50);
  this->info.setGomofont(this->TtfFont[0], 100);
  this->info.setSizefont(this->TtfSize[0]);
  this->str.SetFont(this->info.getGomofont());
}

void		OptionMenu::addPolice(std::string s, std::string n, int sf)
{
  this->TtfFont.push_back(s);
  this->TtfName.push_back(n);
  this->TtfSize.push_back(sf);
}

OptionMenu::~OptionMenu(void)
{
}

void		OptionMenu::run(void)
{
  this->App.Clear();
  if (this->action.find(this->Event.Type) != this->action.end())
    (this->*(this->action.find(this->Event.Type)->second))();
  for (int i = this->items.size() - 1; i >= 0; i--) 
    this->items[i]->draw(this->App);
  for (int i = this->hbutton.size() - 1; i >= 0; i--) 
    this->hbutton[i]->draw(this->App);
  this->str.SetSize(this->info.getSizefont());
  this->str.SetColor(sf::Color(0, 0, 0));
  this->str.SetPosition(260.f, 220.f);
  this->str.SetText(std::string("Regle de trois"));
  this->App.Draw(this->str);
  this->str.SetPosition(260.f, 320.f);
  this->str.SetText(std::string("Regle des 5 cassables"));
  this->App.Draw(this->str);
  this->str.SetPosition(320.f, 420.f);
  this->str.SetText(this->TtfName[this->idFont]);
  this->App.Draw(this->str);
  this->mouseHover();
}

void		OptionMenu::gere_key(void)
{
  switch (this->Event.Key.Code)
    {
    case sf::Key::Escape:
      this->focus = IGUI::MAINMENU;
      this->setInfo();
      break;
    default:
      break;
    }
}

void		OptionMenu::setInfo(void)
{
  if (this->items[0]->getId() == 0)
    this->info.setRule3(false);
  else
    this->info.setRule3(true);
  if (this->items[1]->getId() == 0)
    this->info.setRule5break(false);
  else
    this->info.setRule5break(true);
}

void		OptionMenu::mouse(void)
{
  int		x = this->Input.GetMouseX();
  int		y = this->Input.GetMouseY();

  for (int i = this->items.size() - 2; i >= 0; i--)
    {
      if (y >= this->items[i]->getY() &&
	  y <= this->items[i]->getY() + this->items[i]->getH() &&
	  x >= this->items[i]->getX() &&
	  x <= this->items[i]->getX() + this->items[i]->getW())
	if (this->wait)
	  {
	    this->items[i]->incrState();
	    if (this->items[i]->getId() == 0)
	      this->items[i]->setId(1);
	    else
	      this->items[i]->setId(0);
	    this->wait = 0;
	  }
    }
  for (int i = this->hbutton.size() - 1; i >= 0; i--)
    {
      if (y >= this->hbutton[i]->getY() &&
	  y <= this->hbutton[i]->getY() + this->hbutton[i]->getH() &&
	  x >= this->hbutton[i]->getX() &&
	  x <= this->hbutton[i]->getX() + this->hbutton[i]->getW())
	{
	  this->hbutton[i]->setState(3);
	  if (this->wait)
	    {
	      if (this->hbutton[i]->getId() == 0)
		this->idFont--;
	      else
		this->idFont++;
	      if (this->idFont >= (int)this->TtfFont.size())
		this->idFont = 0;
	      else if (this->idFont < 0)
		this->idFont = this->TtfFont.size() - 1;
	      this->info.setGomofont(this->TtfFont[this->idFont], 100);
	      this->info.setSizefont(this->TtfSize[this->idFont]);
	      this->wait = 0;
	    }
	}
      else
	this->hbutton[i]->setState(0);
    }
}

void		OptionMenu::mouser(void)
{
  this->wait = 1;
}

void		OptionMenu::mouseHover(void)
{
  int		x = this->Input.GetMouseX();
  int		y = this->Input.GetMouseY();

  for (int i = this->hbutton.size() - 1; i >= 0; i--)
    {
      if (y >= this->hbutton[i]->getY() &&
	  y <= this->hbutton[i]->getY() + this->hbutton[i]->getH() &&
	  x >= this->hbutton[i]->getX() &&
	  x <= this->hbutton[i]->getX() + this->hbutton[i]->getW())
	this->hbutton[i]->setState(1);
      else
	this->hbutton[i]->setState(0);
    }
}

void		OptionMenu::resized(void)
{
  this->App.SetSize(1024, 768);
}
